﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class CameraController : MonoBehaviour {

	private IUserInput PI;
	public Image lockDot;
	public bool lockState;

	public float horizontalSpeed;
	public float verticalSpeed;

	private GameObject cameraHandle;
	private GameObject playerHandle;
	private GameObject model;
	private GameObject camera;
	[SerializeField]
	private LockTarget lockTarget;

	private float tempEulerX;


	void Start () {
		cameraHandle = transform.parent.gameObject;
		playerHandle = cameraHandle.transform.parent.gameObject;
		tempEulerX = 20;
		camera = Camera.main.gameObject;
		ActorController ac = playerHandle.GetComponent<ActorController>();
		model = ac.model;
		PI = ac.PI;

		Cursor.lockState = CursorLockMode.Locked;

		lockDot.enabled = false;
		lockState = false;
	}
	void Update()
	{
		if(lockTarget != null)
		{
			lockDot.rectTransform.position = Camera.main.WorldToScreenPoint(lockTarget.obj.transform.position + new Vector3(0, lockTarget.halfHeight, 0));
			if (Vector3.Distance(model.transform.position, lockTarget.obj.transform.position) >= 10.0f)
			{
				lockTarget = null;
				lockDot.enabled = false;
				lockState = false;
			}
		}
	}
	void FixedUpdate () {
		if(lockTarget == null)
		{
		Vector3 tempModelEuler = model.transform.eulerAngles;
		playerHandle.transform.Rotate(Vector3.up, PI.Jright * horizontalSpeed * Time.fixedDeltaTime);
		//cameraHandle.transform.Rotate(Vector3.right, PI.Jup * -verticalSpeed * Time.deltaTime);
		tempEulerX -= -PI.Jup * verticalSpeed * Time.fixedDeltaTime;
		tempEulerX = Mathf.Clamp(tempEulerX, -40, 30);
		cameraHandle.transform.localEulerAngles = new Vector3(tempEulerX, 0, 0);

		model.transform.eulerAngles = tempModelEuler;
		}
		else
		{
			Vector3 tempForward = lockTarget.obj.transform.position - model.transform.position;
			tempForward.y = 0;
			playerHandle.transform.forward = tempForward;
			cameraHandle.transform.LookAt(lockTarget.obj.transform.position);
		}

		camera.transform.position = Vector3.Lerp(camera.transform.position, transform.position,0.2f);
		//camera.transform.eulerAngles = transform.eulerAngles;
		camera.transform.LookAt(cameraHandle.transform);
	}
	public void LockUnLock()
	{
		//try to lock
		Vector3 modelOrigin1 = playerHandle.transform.position;
		Vector3 modelOrigin2 = modelOrigin1 + new Vector3(0, 1f, 0);
		Vector3 boxCenter = modelOrigin2 + playerHandle.transform.forward * 5f;
		Collider[] cols = Physics.OverlapBox(boxCenter, new Vector3(0.5f, 0.5f, 5f),model.transform.rotation,LayerMask.GetMask("Enemy"));
		if(cols.Length == 0)
		{
			lockTarget = null;
			lockDot.enabled = false;
		}
		else
		{
			foreach (var col in cols)
			{
				if(lockTarget!=null && lockTarget.obj == col.gameObject)
				{
					lockTarget = null;
					lockDot.enabled = false;
					lockState = false;
					break;
				}
				else
				{
					lockTarget = new LockTarget(col.gameObject,col.bounds.extents.y);
					lockDot.enabled = true;
					lockState = true;
					break;
				}
			}
		}
		

	}
	class LockTarget
	{
		public GameObject obj;
		public float halfHeight;

		public LockTarget(GameObject _obj,float _halfHeight)
		{
			obj = _obj;
		}
	}
}
